package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.view.VywLabourinfo;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YwLabourinfo;
import com.ruoyi.system.service.IYwLabourinfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * labourinfoController
 * 
 * @author ruoyi
 * @date 2020-04-14
 */
@Controller
@RequestMapping("/system/labourinfo")
public class YwLabourinfoController extends BaseController
{
    private String prefix = "system/labourinfo";

    @Autowired
    private IYwLabourinfoService ywLabourinfoService;

    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("system:labourinfo:view")
    @GetMapping()
    public String labourinfo(ModelMap mmap)
    {
        mmap.put("user", ShiroUtils.getSysUser());
        return prefix + "/labourinfo";
    }

    /**
     * 查询labourinfo列表
     */
    @RequiresPermissions("system:labourinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YwLabourinfo ywLabourinfo)
    {

        startPage();
        List<SysDept> deptList = sysDeptService.selectChildrenDeptById(ywLabourinfo.getDeptId());
        List<YwLabourinfo> list = ywLabourinfoService.selectYwLabourinfoList(ywLabourinfo);
        List<VywLabourinfo> vlist = new ArrayList<>();
        for (YwLabourinfo y : list){
            SysDept dept = sysDeptService.selectDeptById(y.getDeptId());
            VywLabourinfo vy = new VywLabourinfo();
            vy.setDeptId(y.getDeptId());
            vy.setDeptName(dept.getDeptName());
            vy.setId(y.getId());
            vy.setName(y.getName());
            vy.setPassword(y.getPassword());
            vlist.add(vy);
        }
        return getDataTable(vlist);
    }

    /**
     * 导出labourinfo列表
     */
    @RequiresPermissions("system:labourinfo:export")
    @Log(title = "labourinfo", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YwLabourinfo ywLabourinfo)
    {
        List<YwLabourinfo> list = ywLabourinfoService.selectYwLabourinfoList(ywLabourinfo);
        ExcelUtil<YwLabourinfo> util = new ExcelUtil<YwLabourinfo>(YwLabourinfo.class);
        return util.exportExcel(list, "labourinfo");
    }

    /**
     * 新增labourinfo
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("user", ShiroUtils.getSysUser());
        return prefix + "/add";
    }

    /**
     * 新增保存labourinfo
     */
    @RequiresPermissions("system:labourinfo:add")
    @Log(title = "labourinfo", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YwLabourinfo ywLabourinfo)
    {
        return toAjax(ywLabourinfoService.insertYwLabourinfo(ywLabourinfo));
    }

    /**
     * 修改labourinfo
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YwLabourinfo ywLabourinfo = ywLabourinfoService.selectYwLabourinfoById(id);
        SysDept dept = sysDeptService.selectDeptById(ywLabourinfo.getDeptId());
        mmap.put("ywLabourinfo", ywLabourinfo);
        mmap.put("deptinfo",dept);
        return prefix + "/edit";
    }

    /**
     * 修改保存labourinfo
     */
    @RequiresPermissions("system:labourinfo:edit")
    @Log(title = "labourinfo", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YwLabourinfo ywLabourinfo)
    {
        return toAjax(ywLabourinfoService.updateYwLabourinfo(ywLabourinfo));
    }

    /**
     * 删除labourinfo
     */
    @RequiresPermissions("system:labourinfo:remove")
    @Log(title = "labourinfo", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ywLabourinfoService.deleteYwLabourinfoByIds(ids));
    }
}
